package com.hoperun.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import com.hoperun.bean.AdminDictionaryType;
import com.hoperun.dao.AdminDictionaryTypeDAO;
import com.hoperun.framework.ibatis.page.BootGrid;
import com.hoperun.framework.ibatis.page.BootGridPage;
import com.hoperun.framework.query.Criteria;
import com.hoperun.framework.util.Constant;
import com.hoperun.service.IAdminDictionaryTypeSV;

@Service("adminDictionaryTypeSV")
public class AdminDictionaryTypeSVImpl implements IAdminDictionaryTypeSV {
	@Resource
	private AdminDictionaryTypeDAO adminDictionaryTypeDAO;

	@Override
	public void delete(Long id) throws Exception {
		adminDictionaryTypeDAO.delete(id);
	}

	@Override
	public AdminDictionaryType queryById(Long id) throws Exception {
		return adminDictionaryTypeDAO.queryById(id);
	}

	@Override
	public AdminDictionaryType save(AdminDictionaryType adminDictionaryType)
			throws Exception {
		adminDictionaryTypeDAO.insert(adminDictionaryType);
		return adminDictionaryType;
	}

	@Override
	public void update(AdminDictionaryType adminDictionaryType)
			throws Exception {
		adminDictionaryTypeDAO.update(adminDictionaryType);
	}

	@Override
	public void saveOrUpdate(AdminDictionaryType adminDictionaryType)
			throws Exception {
		if (adminDictionaryType == null)
			return;
		if (StringUtils.isNotBlank("" + adminDictionaryType.getDtypeId())) {
			update(adminDictionaryType);
		} else {
			save(adminDictionaryType);
		}
	}

	@Override
	public void saveOrUpdate(List<AdminDictionaryType> list) throws Exception {
		if (CollectionUtils.isNotEmpty(list)) {
			for (AdminDictionaryType adminDictionaryType : list) {
				saveOrUpdate(adminDictionaryType);
			}
		}
	}

	@Override
	public List<AdminDictionaryType> queryByCondition(Criteria criteria)
			throws Exception {
		return adminDictionaryTypeDAO.queryByCondition(criteria);
	}

	@Override
	public int queryCountByCondition(Criteria criteria) throws Exception {
		return adminDictionaryTypeDAO.queryCountByCondition(criteria);
	}

	@Override
	public BootGridPage<AdminDictionaryType> queryPage(Criteria criteria,
			BootGrid<AdminDictionaryType> grid) throws Exception {

		// AdminDictionaryType model = grid.getEntity();

		int totalCount = adminDictionaryTypeDAO.queryCountByCondition(criteria);

		if (Constant.ZERO < totalCount) {

			Map<String, String> sorts = grid.getSort();

			/** 设置排序规则 **/
			if (MapUtils.isNotEmpty(sorts)) {
				Set<String> keySets = sorts.keySet();
				String sort = "";
				for (String key : keySets) {
					sort += " , " + key + " " + sorts.get(key);
				}
				criteria.orderBy(sort.replaceFirst(",", ""));
			}
			if (grid.getCurrent() >= Constant.ZERO) {
				int start = (grid.getCurrent() - Constant.ONE)
						* grid.getRowCount();
				int size = grid.getRowCount();

				criteria.limit(start, size);
			}
			List<AdminDictionaryType> list = adminDictionaryTypeDAO
					.queryByCondition(criteria);
			BootGridPage<AdminDictionaryType> page = new BootGridPage<AdminDictionaryType>(
					grid.getCurrent(), grid.getRowCount(), totalCount, list);

			return page;
		}
		return new BootGridPage<AdminDictionaryType>(grid.getCurrent(),
				grid.getRowCount(), totalCount,
				new ArrayList<AdminDictionaryType>());

	}

	@Override
	public List<AdminDictionaryType> queryBySpecificCondition(Criteria criteria)
			throws Exception {

		List<AdminDictionaryType> types = adminDictionaryTypeDAO
				.queryBySpecificCondition(criteria);

		return types;
	}

}
